//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#pragma package(smart_init)
#pragma resource "*.dfm"

//---------------------------------------------------------------------------
#include "vSCopyRef.h"
//---------------------------------------------------------------------------
__fastcall TCopyRef::TCopyRef(const int &nRefCode,
                              const int &nLogiciel,
                              const int &nVersion,
                              TComponent* Owner)
    : TForm(Owner)
{
    try {
        dmWebHelp = new TdmWebHelp((TComponent*)0);
        m_RefCode   = nRefCode;
        m_asRefNom  = GetRef_Nom(nRefCode);
        m_nLogiciel = nLogiciel;
        m_nVersion  = nVersion;

        eRef->Text = m_asRefNom;
        AjouterListe();
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
__fastcall TCopyRef::~TCopyRef()
{
    try {
        if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//              RECUPERER LE NOM DE LA REFERENCE
//---------------------------------------------------------------------------
AnsiString __fastcall TCopyRef::GetRef_Nom(const int &nRefCode)
{
    AnsiString Ref_Nom = "";
    TZZMySqlQuery * MaRequete = NULL;
    try {

        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text =  (AnsiString)"select ref_nom "+
                                            "from refer "+
                                            "where ref_code = "+nRefCode;
        MaRequete->Open();
        MaRequete->First();

        Ref_Nom = asRequete(0);

        MaRequete->Sql->Clear();
        MaRequete->Close();

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        Ref_Nom = "";}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    return Ref_Nom;
}
//---------------------------------------------------------------------------
//              AJOUTER LES REFERENCES DANS LA COMBO BOX
//---------------------------------------------------------------------------
void __fastcall TCopyRef::AjouterListe()
{
    try {
        dmWebHelp->tRefer->Active = true;
        dmWebHelp->tRefer->First();
        while(!dmWebHelp->tRefer->Eof) {
            if(     (ntRefer(LOG_CODE) == m_nLogiciel)
                &&  (ntRefer(VER_CODE) == m_nVersion)) {
                if(m_asRefNom != astRefer(REF_NOM) ) {
                    if(!VerifierRef(astRefer(REF_NOM)) )
                        cbRef->Items->AddObject( astRefer(REF_NOM) , (TObject*)ntRefer(REF_CODE) );
                }
            }
            dmWebHelp->tRefer->Next();
        }

        if( cbRef->Items->Count == 0 )
            cbRef->Enabled = false;

        cbRef->ItemIndex = 0;
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//              SUPPRIMER LA REFERENCE CIBLE
//---------------------------------------------------------------------------
void __fastcall TCopyRef::SupprimerRefCible( void )
{
    TZZMySqlQuery * MaRequete = NULL;
    try {
        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text =  (AnsiString)"delete from refer where ref_nom = \""+cbRef->Items->Strings[cbRef->ItemIndex]+"\"";
        MaRequete->ExecSql();
        MaRequete->Sql->Clear();

        MaRequete->Sql->Text = "Commit";
        MaRequete->ExecSql();
        MaRequete->Sql->Clear();

        MaRequete->Close();

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
}
//---------------------------------------------------------------------------
//          Vérifier si la référence source possede une adresse
//---------------------------------------------------------------------------
bool __fastcall TCopyRef::VerifierAdresse( void )
{
    bool bRetour = false;
    TZZMySqlQuery * MaRequete = NULL;
    try {
        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text = (AnsiString) "SELECT ref_adresse "+
                                            "FROM refer "+
                                            "WHERE (log_code="+m_nLogiciel+
                                                " AND ver_code="+m_nVersion+")"+
                                                " AND ref_nom =\""+m_asRefNom+"\"";
        MaRequete->Open();
        MaRequete->First();

        if(!asRequete(0).IsEmpty())
            bRetour = true;

        MaRequete->Sql->Clear();
        MaRequete->Close();

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        bRetour = false;}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    return bRetour;
}
//---------------------------------------------------------------------------
//              COPIER LA REFERENCE SOURCE
//---------------------------------------------------------------------------
void __fastcall TCopyRef::bbOkClick(TObject *Sender)
{
    TZZMySqlQuery * MaRequete = NULL;
    try {
        if(cbRef->ItemIndex != AUCUNE_SELECTION) {
            SupprimerRefCible();

            MaRequete = new TZZMySqlQuery( (TComponent*)0);
            MaRequete->Database = dmWebHelp->dbWebHelp;

            MaRequete->Sql->Text = (AnsiString) "SELECT ref_sous_nom,"+         //0
                                                        "ref_commentaire,"+     //1
                                                        "ref_adresse,"+         //2
                                                        "ref_attente "+         //3
                                                "FROM refer "+
                                                "WHERE (log_code="+m_nLogiciel+
                                                    " AND ver_code="+m_nVersion+")"+
                                                    " AND ref_nom =\""+m_asRefNom+"\"";
            MaRequete->Open();
            MaRequete->First();

            dmWebHelp->tRefer->Active = true;
            while(!MaRequete->Eof) {
              dmWebHelp->tRefer->Insert();
              ntRefer(LOG_CODE)           = m_nLogiciel;
              ntRefer(VER_CODE)           = m_nVersion;
              astRefer(REF_NOM)           = cbRef->Items->Strings[cbRef->ItemIndex];
              astRefer(REF_SOUS_NOM)      = asRequete(0);
              astRefer(REF_COMMENTAIRE)   = asRequete(1);
              astRefer(REF_ADRESSE)       = asRequete(2);
              ntRefer(REF_ATTENTE)        = nRequete(3);
              dmWebHelp->tRefer->Post();
              MaRequete->Next();
            }
            dmWebHelp->trWebHelp->Commit();

            MaRequete->Sql->Clear();
            MaRequete->Close();
        }
        else {
            MessageDlg("Veuillez sélectionner tous les champs", mtWarning, TMsgDlgButtons() << mbOK, 0);
            ModalResult = mrNone;
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
}
//---------------------------------------------------------------------------
//              EVITER LES DOUBLONS DANS LA COMBO BOX
//---------------------------------------------------------------------------
bool __fastcall TCopyRef::VerifierRef(const AnsiString &asRef)
{
    bool bRetour = false;
    try {
        int idx = cbRef->Items->Count;
        for( int i=0; i<idx; i++) {
            if(asRef == cbRef->Items->Strings[i]) {
                bRetour = true;
            }
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        bRetour = false;
    }
    return bRetour;
}
//---------------------------------------------------------------------------
